package com.xs.bookspoststation.StudentActivity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.xs.bookspoststation.R;
import com.xs.bookspoststation.UserActivity.AddUserActivity;
import com.xs.bookspoststation.UserActivity.OperateUserActivity;
import com.xs.bookspoststation.db.DBHelperStudent;
import com.xs.bookspoststation.db.DBHelperUser;

public class AddStudentActivity extends AppCompatActivity implements View.OnClickListener {
    //定义对象
    private EditText edit_add_sno,edit_add_sname,edit_add_spwd,
            stu_sphone,edit_add_sclass,edit_add_spro,edit_add_sde;
    private RadioGroup stu_add_radioGroup2;
    private RadioButton stu_add_radioButton1,stu_add_radioButton2;
    private Button btn_add_stu_cancel,btn_add_stu;
    //定义一个数据库帮助类
    private DBHelperStudent dbHelperStudent;
    //定义一个数据库操作类
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_student);
        //操作对象的方法
        initView();
    }

    private void initView() {
        //获取学号控件
        edit_add_sno = findViewById(R.id.edit_add_sno);
        //获取姓名控件
        edit_add_sname = findViewById(R.id.edit_add_sname);
        //获取密码控件
        edit_add_spwd = findViewById(R.id.edit_add_spwd);
        //获取电话控件
        stu_sphone = findViewById(R.id.stu_sphone);
        //获取班级控件
        edit_add_sclass = findViewById(R.id.edit_add_sclass);
        //获取专业控件
        edit_add_spro = findViewById(R.id.edit_add_spro);
        //获取系部控件
        edit_add_sde = findViewById(R.id.edit_add_sde);
        //获取性别
        stu_add_radioGroup2 = findViewById(R.id.stu_add_radioGroup2);
        //获取性别女控件
        stu_add_radioButton2 = findViewById(R.id.stu_add_radioButton2);
        //获取性别男控件
        stu_add_radioButton1 = findViewById(R.id.stu_add_radioButton1);
        //获取返回控件
        btn_add_stu_cancel = findViewById(R.id.btn_add_stu_cancel);
        //获取添加控件
        btn_add_stu = findViewById(R.id.btn_add_stu);
        //点击事件
        btn_add_stu_cancel.setOnClickListener(this);
        btn_add_stu.setOnClickListener(this);
        //实例化数据库帮助类对象
        dbHelperStudent = new DBHelperStudent(AddStudentActivity.this,"student.db",null,1);
        //创建数据库并获取数据库的读写权限
        db = dbHelperStudent.getWritableDatabase();
    }
    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add_stu_cancel:
                Intent intent=new Intent(AddStudentActivity.this, OperateStudentActivity.class);
                Toast.makeText(getApplicationContext(), "返回上一界面", Toast.LENGTH_LONG).show();
                startActivity(intent);
                break;
            case R.id.btn_add_stu:
                goAdd();
                break;
        }
    }
    /**
     * 添加
     */
    private void goAdd() {
        // 获取用户输入的数据
        String stu_sno = edit_add_sno.getText().toString().trim();
        String stu_name = edit_add_sname.getText().toString().trim();
        String stu_pwd = edit_add_spwd.getText().toString().trim();
        String stu_phone = stu_sphone.getText().toString().trim();
        String stu_class = edit_add_sclass.getText().toString().trim();
        String stu_pro = edit_add_spro.getText().toString().trim();
        String stu_de = edit_add_sde.getText().toString().trim();

        //获取性别值
        String strSex = "";
        //根据选中的id进行判断
        switch (stu_add_radioGroup2.getCheckedRadioButtonId()){
            case R.id.stu_add_radioButton1:
                strSex = stu_add_radioButton1.getText().toString();
                break;
            case R.id.stu_add_radioButton2:
                strSex = stu_add_radioButton2.getText().toString();
                break;
        }

        boolean AddStudent = true;
        dbHelperStudent = new DBHelperStudent(AddStudentActivity.this,"student.db",null,1);
        db = dbHelperStudent.getWritableDatabase();
        if (stu_sno.equals("") || stu_name.equals("") || stu_pwd.equals("")){
            Toast.makeText(AddStudentActivity.this, "您还有信息未填写！！！", Toast.LENGTH_SHORT).show();
        }else if(stu_pwd.length() < 6 || stu_pwd.length() > 6){
            Toast.makeText(AddStudentActivity.this, "密码必须为六位数", Toast.LENGTH_SHORT).show();
        }else {
            Cursor cursor = db.query("studentInfo",new String[]{"sno"},null,null,null,null,null);
            while (cursor.moveToNext()){
                if (stu_sno.equals(cursor.getString(cursor.getColumnIndex("sno")))){
                    Toast.makeText(AddStudentActivity.this, "该学号已存在", Toast.LENGTH_SHORT).show();
                    AddStudent = false;
                    break;
                }
            }
            if (AddStudent){
                //存储类，只能为String类型
                ContentValues values = new ContentValues();
                values.put("sno",stu_sno);
                values.put("sname",stu_name);
                values.put("spwd",stu_pwd);
                values.put("sex",strSex);
                values.put("sphone",stu_phone);
                values.put("sclass",stu_class);
                values.put("spro",stu_pro);
                values.put("sde",stu_de);
                db.insert("studentInfo",null,values);
                db.close();//关闭数据库
                AlertDialog alertDialog = new AlertDialog.Builder(AddStudentActivity.this).create();
                alertDialog.setTitle("添加成功");
                alertDialog.setMessage("是否查看？");
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.show();
            }
        }
    }
}
